package game;

public class Board {
	public static final int BOARD_SIZE = 9;
	private int[][] board;
	private int boardSize = BOARD_SIZE;//Foi definido para manter a compatibilidade, evitanto erros no desenvolvimento
	
	public Board(int[][] board) {
		this.board = board;
	}

	public Board(int... valores) {
		board = new int[this.boardSize][this.boardSize];
		int i = 0;
		
		for(int row = 1; row < this.boardSize; row++) {
			for(int column = 1; column < this.boardSize; column++) {
				this.board[row][column] = valores[i];
				i++;
			}
		}
	}
	
	public Board() {
		board = new int[this.boardSize][this.boardSize];
	}
	
	public String toStringArray(String separador) {
		StringBuffer str = new StringBuffer();
		
		for(int row = 1; row < this.boardSize; row++) {
			for(int column = 1; column < this.boardSize; column++) {
				str.append(this.board[row][column]);
				str.append(separador);
			}
		}
		
		return str.toString().substring(0, str.length() - 1);
	}
	
	public int getCellValue(int row, int column){
		return board[row][column];
	}
	
	public void setCellValue(int row, int column, int value){
		board[row][column] = value;
	}
	
	//Contagem da quantidade das celulas pela cor passada por parametro
	public int getCellAmountByColor(int color){
		int colorAmount = 0;
		
		for(int row = 1; row < this.boardSize; row ++){
			for(int column = 1; column < this.boardSize; column++){
				if(getCellValue(row, column) == color){
					colorAmount++;
				}
			}
		}
		
		return colorAmount;
	}
	
	@Override
	public boolean equals(Object obj) {
		Board board = (Board)obj;
		
		for(int row = 1; row < this.boardSize; row ++){
			for(int column = 1; column < this.boardSize; column++){
				if(this.board[row][column] != board.getBoard()[row][column]){
					return false;
				}
			}
		}
		
		return true;
	}
	
	public Board cloneBoard() {
		return new Board(cloneBoard(this.board));
	}
	
	private int[][] cloneBoard(int[][] board) {
		int[][] retorno = new int[this.boardSize][this.boardSize];
		
		for(int row = 1; row < this.boardSize; row ++) {
			for(int column = 1; column < this.boardSize; column++) {
				retorno[row][column] = board[row][column];
			}
		}
		
		return retorno;
	}
	
	public int getBoardSize() {
		return boardSize;
	}

	public void setBoardSize(int boardSize) {
		this.boardSize = boardSize;
	}
	
	public int[][] getBoard() {
		return board;
	}

	public void setBoard(int[][] board) {
		this.board = board;
	}
}